package kr.saeildamil.androidStudy_second.multimedia;

import java.io.IOException;

import kr.saeildamil.androidStudy_second.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

public class MPTest extends Activity {
	MediaPlayer mPlayer;
	String mSDPath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_mptest);

		mPlayer = MediaPlayer.create(this, R.raw.dingdong);
		mSDPath = Environment.getExternalStorageDirectory().getAbsolutePath();

	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if(mPlayer != null){
			mPlayer.release();
			mPlayer = null;
		}
	}

	public void mOnClick(View v) {
		MediaPlayer player;

		switch (v.getId()) {
		case R.id.btn1: // resource 재생
			player = MediaPlayer.create(this, R.raw.dingdong);
			player.start();
			break;
		case R.id.btn2: // File 재생
			player = new MediaPlayer();
			try {
				player.setDataSource(mSDPath + "/Muse-05-Plug In Baby-128.mp3.mp3");
				player.prepare();
				player.start();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		case R.id.btn3:	// Stream 재생
			player = new MediaPlayer();
			Uri uri = Uri.parse("http://www.soen.kr/data/saemaull.mp3");
			try {
				player.setDataSource(this, uri);
				player.prepare();
				player.start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		case R.id.btn4: // 미리준비된 객체로 생성
			mPlayer.seekTo(0);
			mPlayer.start();
			break;
		case R.id.btn5: //준비하지 않은 (prepare()를 호출하지않음) 상태에서 생성
			player = new MediaPlayer();
			try {
				player.setDataSource(mSDPath + "Muse-05-Plug In Baby-128.mp3.mp3");
				player.start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		case R.id.btn6: //다른 파일 열기
			player = MediaPlayer.create(this, R.raw.dingdong);
			// player.reset();
			try {
				player.setDataSource(mSDPath + "Muse-05-Plug In Baby-128.mp3.mp3");
			} catch (Exception e) {
				Toast.makeText(this, mSDPath, Toast.LENGTH_LONG).show();
			} 

		default:
			break;
		}
	}
}
